package com.smartStatus.log.config;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName : LogConfiguration
 * @Author : lby
 * @Date: 2020/11/19 10:09
 * @Description : 控制台log日志打印
 */
@Slf4j
public class LogConfiguration {
    /**
     * 方法执行前
     */
    public static void doBefore(JoinPoint point, String serialNumber) {
        ServletRequestAttributes attribute = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attribute.getRequest();
        log.info("流水号：[{}]=====================>接收请求 开始<=====================", serialNumber);
        log.info("流水号：[{}]请求URL：{}", serialNumber, request.getRequestURL().toString());
        log.info("流水号：[{}]请求类型：{}", serialNumber, request.getMethod());
        log.info("流水号：[{}]=====================>打印请求参数 开始<=====================", serialNumber);
        Object[] args = point.getArgs();
        Integer count = 0;
        for (Object arg : args) {
            count++;
            try {
                log.info("流水号：[{}][{}]{}：{}", serialNumber, count, arg.getClass().getSimpleName(), arg);
            } catch (Exception e) {
            }
        }
        log.info("流水号：[{}]=====================>打印请求参数 结束<=====================", serialNumber);
        log.info("流水号：[{}]=====================>接收请求 结束<=====================", serialNumber);
    }


    /**
     * 方法执行之后
     */
    public static void doLast(Object ret, String serialNumber) {
        log.info("流水号：[{}]=====================>请求处理 结束 流水号：{}<=====================", serialNumber, serialNumber);
        log.info("流水号：[{}]返回数据：{}", serialNumber, ret);
    }
}
